package api

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"z-blog/e"
)

var mapData = [15][15]string{}

func RestartGame(c *gin.Context) {
	code := e.SUCCESS
	mapData = [15][15]string{}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": mapData,
	})
}

func GetMapData(c *gin.Context) {
	code := e.SUCCESS

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": mapData,
	})
}

func SetChess(c *gin.Context) {
	code := e.SUCCESS
	x, _ := strconv.Atoi(c.Query("x"))
	y, _ := strconv.Atoi(c.Query("y"))
	color := c.Query("color")

	// 黑色为1， 白色为0
	if mapData[x][y] != "" {
		code = e.ErrorChessPosition
	} else {
		if color == "b" {
			mapData[x][y] = "b"
		} else {
			mapData[x][y] = "w"
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  e.GetMsg(code),
		"data": mapData,
	})
}
